package com.qf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.JsonObject;
import com.qf.mapper.BookMapper;
import com.qf.myexception.ServiceException;
import com.qf.pojo.*;
import com.qf.service.BookService;
import com.qf.service.PatientService;
import com.qf.utils.Constant;
import com.qf.utils.GetDate;
import com.qf.utils.ResultMsg;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import javax.lang.model.SourceVersion;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, BookingPojo> implements BookService {

    @Autowired
    BookMapper bookMapper;

    @Autowired
    PatientService patientService;

    @Override
    public ResultMsg findAll(Integer current, Integer pageSize) {
        QueryWrapper<BookingPojo> wrapper = new QueryWrapper<>();
        wrapper.eq("book_delete", 0);
        Page<BookPatientVo> page = new Page<>(current, pageSize);
        Page<BookPatientVo> page1 = bookMapper.getPageVo(page, wrapper);
        if (page1 != null) {
            return ResultMsg.SUCCESS(page1);
        }
        throw new ServiceException(201, "查预约失败");
    }

    @Override
    public ResultMsg sure(int bookId) {
        UpdateWrapper<BookingPojo> wrapper = new UpdateWrapper<>();
        wrapper.eq("book_id", bookId);
        wrapper.set("book_status", 1);
        boolean update = this.update(wrapper);
        if (update) {
            return ResultMsg.SUCCESS();
        }
        throw new ServiceException(201, "确认异常");
    }

    @Override
    public ResultMsg cancel(int bookId) {
        UpdateWrapper<BookingPojo> wrapper = new UpdateWrapper<>();
        wrapper.eq("book_id", bookId);
        wrapper.set("book_status", 2);
        boolean update = this.update(wrapper);
        if (update) {
            return ResultMsg.SUCCESS();
        }
        throw new ServiceException(201, "取消异常");
    }

    @Override
    public ResultMsg findByDay(String time) {
        QueryWrapper<BookingPojo> wrapper = new QueryWrapper<>();
        wrapper.eq("book_delete", 0);
        if (time != null) {
            Calendar calendar = Calendar.getInstance();
            String[] split = time.split("-");
            calendar.set(Calendar.YEAR, Integer.valueOf(split[0]));
            calendar.set(Calendar.MONTH, Integer.valueOf(split[1]) - 1);
            calendar.set(Calendar.DATE, Integer.valueOf(split[2]));
            System.out.println("split = " + calendar.getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String startTime = sdf.format(GetDate.getTimesmorning(calendar));
            String endTime = sdf.format(GetDate.getTimesnight(calendar));
            wrapper.between("book_date", startTime, endTime);
        }
        List<BookPatientVo> list = bookMapper.findByDay(wrapper);
        if (list != null) {
            return ResultMsg.SUCCESS(list);
        }
        throw new ServiceException(201, "查看失败");
    }

    @Override
    public ResultMsg findByWeek(String time) {
        QueryWrapper<BookingPojo> wrapper = new QueryWrapper<>();
        wrapper.eq("book_delete", 0);
        if (time != null) {
            Calendar calendar = Calendar.getInstance();
            String[] split = time.split("-");
            calendar.set(Calendar.YEAR, Integer.valueOf(split[0]));
            calendar.set(Calendar.MONTH, Integer.valueOf(split[1]) - 1);
            calendar.set(Calendar.DATE, Integer.valueOf(split[2]));
            System.out.println("split = " + calendar.getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String startTime = sdf.format(GetDate.getTimesWeekmorning(calendar));
            String endTime = sdf.format(GetDate.getTimesWeeknight(calendar));
            wrapper.between("book_date", startTime, endTime);
        }
        List<BookPatientVo> list = bookMapper.findByDay(wrapper);
        if (list != null) {
            return ResultMsg.SUCCESS(list);
        }
        throw new ServiceException(201, "查看失败");
    }

    @Override
    public ResultMsg findByMonth(String time) {
        QueryWrapper<BookingPojo> wrapper = new QueryWrapper<>();
        wrapper.eq("book_delete", 0);
        if (time != null) {
            Calendar calendar = Calendar.getInstance();
            String[] split = time.split("-");
            calendar.set(Calendar.YEAR, Integer.valueOf(split[0]));
            calendar.set(Calendar.MONTH, Integer.valueOf(split[1]) - 1);
            System.out.println("split = " + calendar.getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String startTime = sdf.format(GetDate.getTimesMonthmorning(calendar));
            String endTime = sdf.format(GetDate.getTimesMonthnight(calendar));
            wrapper.between("book_date", startTime, endTime);
        }
        List<BookPatientVo> list = bookMapper.findByDay(wrapper);
        if (list != null) {
            return ResultMsg.SUCCESS(list);
        }
        throw new ServiceException(201, "查看失败");
    }

    @Override
    public ResultMsg add(Map<String, Object> map) {
//        JSONObject jsonObject=JSONObject.parseObject(json);
//        PatientPojo patient = jsonObject.getObject("patient", PatientPojo.class);
//        BookingPojo booking = jsonObject.getObject("booking", BookingPojo.class);
        PatientPojo patient = new PatientPojo();
        patient.setPatientName((String) map.get("patientName"));
        patient.setPatientAge((Integer) map.get("patientAge"));
        patient.setPatientSex((String) map.get("patientSex"));
        patient.setUserId((Integer) map.get("userId"));
        patient.setPatientTel((String) map.get("patientTel"));
        patient.setPatientCard((String) map.get("patientCard"));
        patient.setPatientStatus(0);
        BookingPojo booking = new BookingPojo();
        if (patient != null) {
            ResultMsg resultMsg = patientService.addPatient(patient);
            if (resultMsg != null) {
                PatientPojo pa = (PatientPojo) resultMsg.getData();
                booking.setPatientId(pa.getPatientId());
            }
        }
        booking.setBookRemark((String) map.get("bookRemark"));
        booking.setBookItems((String) map.get("bookItems"));
        booking.setBookDate(new Timestamp(new java.util.Date().getTime()));
        booking.setBookDelete(0);
        booking.setBookStatus(0);
        boolean save = this.save(booking);
        if (save) {
            return ResultMsg.SUCCESS();
        }
        throw new ServiceException(201, "新增失败");
    }

    @Override
    public ResultMsg del(int bookId) {
        UpdateWrapper<BookingPojo> wrapper = new UpdateWrapper<>();
        wrapper.eq("book_id", bookId);
        wrapper.set("book_delete", 1);
        boolean update = this.update(wrapper);
        if (update) {
            return ResultMsg.SUCCESS();
        }
        throw new ServiceException(201, "删除失败");
    }

    @Override
    public ResultMsg restore(int bookId) {
        UpdateWrapper<BookingPojo> wrapper = new UpdateWrapper<>();
        wrapper.eq("book_id", bookId);
        wrapper.set("book_delete", 0);
        boolean update = this.update(wrapper);
        if (update) {
            return ResultMsg.SUCCESS();
        }
        throw new ServiceException(201, "还原失败");
    }

    @Override
    public ResultMsg delById(int bookId) {
        QueryWrapper<BookingPojo> wrapper = new QueryWrapper<>();
        wrapper.eq("book_id", bookId);
        boolean remove = this.remove(wrapper);
        if (remove) {
            return ResultMsg.SUCCESS();
        }
        throw new ServiceException(201, "删除失败");
    }

    @Override
    public ResultMsg findByKeyword(String keyword) {
        QueryWrapper<BookingPojo> wrapper = new QueryWrapper<>();
        wrapper.eq("book_delete", 0);
        wrapper.and(queryWrapper -> queryWrapper.like("patient_name", keyword).or().like("patient_tel", keyword));
        List<BookPatientVo> list = bookMapper.findByNameOrTel(wrapper);
        if (list != null) {
            return ResultMsg.SUCCESS(list);
        }
        throw new ServiceException(201, "查看失败");
    }

    @Override
    public ResultMsg findAll2(Map<String, Object> maps) {
        Page<BookPatientVo> page = null;
        QueryWrapper<BookingPojo> wrapper = new QueryWrapper<>();
        if (maps.size() != 0) {
            if (maps.get("init") != null) {
                String string = maps.get("init").toString();
                Integer init = Integer.valueOf(string);
                wrapper.eq("book_delete", init);
            }
            int current = Integer.parseInt(maps.get("current").toString());
            int pageSize = Integer.parseInt(maps.get("pageSize").toString());
            page = new Page<>(current, pageSize);
            String startTime = (String) maps.get("startTime");
            String endTime = (String) maps.get("endTime");
            if (startTime != null && endTime != null && !startTime.equals("") && !endTime.equals("")) {
                wrapper.between("book_date", startTime + " 00:00:00", endTime + " 23:59:59");
            }
            Object status = maps.get("bookStatus");
            if (status != null) {
                if (!status.equals("all")) {
                    int status1 = Integer.parseInt(status.toString());
                    wrapper.eq("book_status", status1);
                }
            }
            Page<BookPatientVo> page1 = bookMapper.getPageVo(page, wrapper);
            if (page1 != null) {
                return ResultMsg.SUCCESS(page1);
            }
            throw new ServiceException(201, "查预约失败");
        }
        throw new ServiceException(201, "参数不能为空");
    }

    @Override
    public ResultMsg findByUid(int userId) {
        QueryWrapper<BookingPojo> wr = new QueryWrapper<>();
        wr.eq("book_delete", 0);
        wr.eq("user_id", userId);
        List<BookPatientVo> list = bookMapper.findByDay(wr);
        if (list != null) {
            return ResultMsg.SUCCESS(list);
        }
        throw new ServiceException(201, "查看失败");
    }

    @Override
    public ResultMsg findByTomorrow() {
        QueryWrapper<BookingPojo> wrapper = new QueryWrapper<>();
        wrapper.eq("book_delete", 0);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR_OF_DAY, 1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = sdf.format(GetDate.getTimesmorning(calendar));
        String endTime = sdf.format(GetDate.getTimesnight(calendar));
        wrapper.between("book_date", startTime, endTime);
        List<BookPatientVo> list = bookMapper.findByDay(wrapper);
        if (list != null) {
            return ResultMsg.SUCCESS(list);
        }
        throw new ServiceException(201, "查看失败");
    }

    @Override
    public ResultMsg clear() {
        QueryWrapper<BookingPojo> wrapper = new QueryWrapper<>();
        wrapper.eq("book_delete", Constant.DELETED);
        boolean remove = this.remove(wrapper);
        if (remove)
            return ResultMsg.SUCCESS();
        return ResultMsg.FAILED(Constant.LZY + 1, "清空失败");
    }
}
